import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;

public abstract class SortAlgorithmAdapter {
	
	private static int DEFAULT_ARRAY_LENGTH = 20;

	private ArrayList<Integer> sequence = new ArrayList<Integer>();
	protected int[] array = new int[DEFAULT_ARRAY_LENGTH];
	protected int arrayLen = 0;


	protected abstract void sortArray();
	
	public void doSort() {
		this.copy2Array();
		this.sortArray();
	}
	
	/**
	 * @Input  n/a
	 * @Output n/a
	 * @Description: Read input, and store the values into internal sequence of Integers.
	 * @throws IOException
	 */
	public void doInput() throws IOException {
		System.out.println("Input a sequence of numbers (Separated by comma, and ending with return)");

		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String inputString = br.readLine(); 

		String[] tokens = inputString.split("[,. ]");

		for(String text : tokens) {
			if ( text == null || text.isEmpty()) 
				continue;
			
			assert text!= null && !text.isEmpty();
			
			System.out.println("Before replacing, text is -" + text + "-.");
			text.replaceAll("[^\\d]", "");
			System.out.println("After replacing, text is -" + text + "-.");
			
			if ( text == null || text.isEmpty())
				continue;
			
			assert text != null && !text.isEmpty();			
			
			//sequence.add(Integer.parseInt(text));
			System.out.println("integer.valueOf(txt) is " + Integer.valueOf(text));
			this.sequence.add(Integer.valueOf(text));
		}
		System.out.println(".");
		
		
		System.out.println(this.sequence);
		
		
		System.out.println("The length of sequence is " + sequence.size());
	}
	
	public void showArray() {
		//assert this.array.length == this.arrayLen;
		
		//System.out.println(this.array.toString());
		for (int i = 0; i < this.arrayLen; i++) {
			if ( i < this.arrayLen - 1) {
				System.out.print(this.array[i] + ",");
			}
			else {
				System.out.println(this.array[i] + ".");
			}
		}
	}
	
	/**
	 * Description: Copy integer values from the sequence of Integer classes to int[] array.
	 */
	private void copy2Array() {
		int inputLen = this.sequence.size();
		
		// Make sure that the length of array is long enough.
		if ( inputLen >  DEFAULT_ARRAY_LENGTH ) {
			array = new int[inputLen];
		}

		int i = 0;
		this.arrayLen = 0;
		for (Integer value : this.sequence) {
			this.arrayLen++;
			
			System.out.println(value);
			
			System.out.println("value.intValue is " + value.intValue());
			array[i++] = value.intValue();

			System.out.println("array[" + (i-1) + "] = " + array[i-1]);
		}
		
		System.out.println(">>>> this.arrayLen is " + this.arrayLen + " and this.array.length is " + this.array.length + ". <<<<<<");
		//assert this.arrayLen == this.array.length;
	}
	
}
